package cn.littlehx.spring_boot_test2_2.api;

import antlr.StringUtils;
import cn.littlehx.spring_boot_test2_2.core.PageUtils;
import cn.littlehx.spring_boot_test2_2.domain.Student;
import cn.littlehx.spring_boot_test2_2.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@Slf4j
@RequestMapping("/api")
public class StudentApiController {

    /**
     * url 为/api/student
     */
    @Autowired
    private StudentService studentService;

    @GetMapping(value = "/student")
    public List<Student> list() {
        return studentService.findALL();
    }

    @GetMapping(value = "/student/{name}")
    public List<Student> getByNameLike(@PathVariable String name) {
        return studentService.findByNameLike(name);
    }

    /**
     * 新增学生的方法
     *
     * @param student
     * @return
     */
    @PostMapping("/student")
    public Student insert(Student student) {
        Student insert = studentService.insert(student);
        return insert;
    }

    /**
     * 修改学生的方法
     *
     * @param student
     * @return
     */
    @PutMapping("/student")
    public Student update(Student student) {
        return studentService.update(student);
    }

    @DeleteMapping("/student")
    public void delete(Student student) {
        studentService.delete(student.getId());
    }

    @GetMapping("/student/id/{id}")
    public Student getById(@PathVariable Integer id) {
        Student student = studentService.findById(id);
        if (student == null) {
            return null;
        }
        student.setPassword("");
        return student;
    }

    /**
     * 分页读取全部内容
     *
     * @return
     */
    @GetMapping("/student/getByPage")
    public PageUtils getByPage(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer size,
                               @RequestParam(defaultValue = "") String name, @RequestParam(defaultValue = "") String no, @RequestParam(defaultValue = "") String sex) {
//        测试
        Sort sort = Sort.by("id").descending();
        if (name.equals("") && sex.equals("") && no.equals("")) {
            PageRequest pageRequest = PageRequest.of(page, size, sort);
            Page<Student> studentPage = studentService.findAll(pageRequest);
            log.info("走了这里");
            return new PageUtils(studentPage.getContent(), studentPage.getTotalElements());
        } else {
            ExampleMatcher exampleMatcher = ExampleMatcher.matching();

            Student student = new Student();
            if (!name.equals("")) {
                student.setName(name);
                exampleMatcher = exampleMatcher.withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains());
            }
            if (!sex.equals("")) {
                student.setSex(Integer.valueOf(sex));
                exampleMatcher = exampleMatcher.withMatcher("sex", ExampleMatcher.GenericPropertyMatchers.exact());
            }
            if (!no.equals("")) {
                student.setNo(no);
                exampleMatcher = exampleMatcher.withMatcher("no", ExampleMatcher.GenericPropertyMatchers.startsWith());
            }



            PageRequest pageRequest = PageRequest.of(page, size, sort);
            log.info("查询的姓名是" + student.getName() + " 性别是" + student.getSex() + " no是" + student.getNo());
            //进行模糊查询

            Example<Student> studentExample = Example.of(student, exampleMatcher);
            Page<Student> studentPage = studentService.findAll(studentExample, pageRequest);
            return new PageUtils(studentPage.getContent(), studentPage.getTotalElements());

        }

    }


}
